/***************************************************
> Copyright (C) 2024 ==KINGYI== All rights reserved.
> File Name: 共享收8.6.c
> Author:
> Mail: 
> Created Time: Mon 05 Aug 2024 08:35:40 PM PDT
***************************************************/

//通过共享内存传输一个学员信息

#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <stdio.h>
#include <sys/shm.h>
#include <unistd.h>

int main(int argc,const char* argv[])
{
	key_t key = 0;
	int shmid = 0;
	int ret = 0;
	key = ftok("./music/",1);
	if(key == -1)
	{
		perror("ftok");
		exit(-1);
	}
	shmid = shmget(key,1024,IPC_CREAT|0600);
	if(shmid == -1)
	{
		perror("shmget");
		exit(-1);
	}
	char *str = NULL;
	str =shmat(shmid,NULL,0);
	if(str == (void *)-1)
	{
		perror("shmat");
		exit(-1);
	}
	char dest[1024] = {0}; 
	sprintf(dest,"%d\n%s\n%d\n",111,"yx",24);
	printf("%s\n",dest);
	strcpy(str,dest);
	ret = shmdt(str);
	if(ret == -1)
	{
		perror("shmdt");
	}
	sleep(6);
	ret = shmctl(shmid,IPC_RMID,NULL);
	if(ret == -1)
	{
		perror("shmctl");
	}
	return 0;
}
